package jdbc;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class showuser
 */
@WebServlet("/src/showuser")
public class showuser extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public showuser() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setHeader("Access-Control-Allow-Origin", "*");//模板
	    response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");//模板
	    response.setHeader("Access-Control-Allow-Headers", "Content-Type");//模板
	    //设置编码格式
	    request.setCharacterEncoding("UTF-8");//模板
	    response.setCharacterEncoding("UTF-8");//模板
		
	    String current_page = request.getParameter("current_page1");
	    String recordsperpage = request.getParameter("recordsperpage1");
	    String user = request.getParameter("user");
	    String search = request.getParameter("search");
	    System.out.println(search);
	    int currentPage = Integer.parseInt(current_page);
	    int recordsPerpage = Integer.parseInt(recordsperpage);
	    hello a = new hello();//模板
		try {//模板
			ResultSet b;
			if (search!=null) {
				if(user.equals("student")) {
					System.out.println("1");
					b = a.doselect("SELECT s.student_id,s.student_name,s.student_username,s.student_password "
						+ "from student s "
						+ "where s.student_id like ? or s.student_name like ? or s.student_username like ? or s.student_password like ? ","%"+search+"%","%"+search+"%","%"+search+"%","%"+search+"%");
			    int rowCount = 0;
			    if (b.last()) {
			        rowCount = b.getRow();
			        b.beforeFirst(); // 将指针移回起始位置
			        response.getWriter().write(String.valueOf(rowCount)+";");
			    }
				b = a.doselect("SELECT s.student_id,s.student_name,s.student_username,s.student_password "
						+ "from student s "
						+ "where s.student_id like ? or s.student_name like ? or s.student_username like ? or s.student_password like ? "
						+ "LIMIT ?, ?", "%"+search+"%","%"+search+"%","%"+search+"%","%"+search+"%", (currentPage-1)*recordsPerpage, recordsPerpage);
				}
				else if(user.equals("teacher")) {
					b = a.doselect("SELECT t.teacher_id,t.teacher_name,t.teacher_username,t.teacher_password "
							+ "from teacher t "
							+ "where t.teacher_id like ? or t.teacher_name like ? or t.teacher_username like ? or t.teacher_password like ? ","%"+search+"%","%"+search+"%","%"+search+"%","%"+search+"%");
					int rowCount = 0;
				    if (b.last()) {
				        rowCount = b.getRow();
				        b.beforeFirst(); // 将指针移回起始位置
				        response.getWriter().write(String.valueOf(rowCount)+";");
				    }
					b = a.doselect("SELECT t.teacher_id,t.teacher_name,t.teacher_username,t.teacher_password "
							+ "from teacher t "
							+ "where t.teacher_id like ? or t.teacher_name like ? or t.teacher_username like ? or t.teacher_password like ? "
							+ "LIMIT ?, ?", "%"+search+"%","%"+search+"%","%"+search+"%","%"+search+"%", (currentPage-1)*recordsPerpage, recordsPerpage);
					
				}
				else{
				b = a.doselect("SELECT s.secretary_id,s.secretary_name,s.secretary_username,s.secretary_password "
						+ "from secretary s "
						+ "where s.secretary_id like ? or s.secretary_name like ? or s.secretary_username like ? or s.secretary_password like ? ","%"+search+"%","%"+search+"%","%"+search+"%","%"+search+"%");

				int rowCount = 0;
			    if (b.last()) {
			        rowCount = b.getRow();
			        b.beforeFirst(); // 将指针移回起始位置
			        response.getWriter().write(String.valueOf(rowCount)+";");
			    }
				b = a.doselect("SELECT s.secretary_id,s.secretary_name,s.secretary_username,s.secretary_password "
						+ "from secretary s "
						+ "where s.secretary_id like ? or s.secretary_name like ? or s.secretary_username like ? or s.secretary_password like ? "
						+ "LIMIT ?, ?", "%"+search+"%","%"+search+"%","%"+search+"%","%"+search+"%", (currentPage-1)*recordsPerpage, recordsPerpage);		
			}
		}		
			else {
				if(user.equals("student")) {
					b = a.doselect("SELECT s.student_id,s.student_name,s.student_username,s.student_password "
						+ "from student s ");
			    int rowCount = 0;
			    if (b.last()) {
			        rowCount = b.getRow();
			        b.beforeFirst(); // 将指针移回起始位置
			        response.getWriter().write(String.valueOf(rowCount)+";");
			    }
				b = a.doselect("SELECT s.student_id,s.student_name,s.student_username,s.student_password "
						+ "from student s "
						+ "LIMIT ?, ?", (currentPage-1)*recordsPerpage, recordsPerpage);
				}
				else if(user.equals("teacher")) {
					b = a.doselect("SELECT t.teacher_id,t.teacher_name,t.teacher_username,t.teacher_password "
							+ "from teacher t ");
					int rowCount = 0;
				    if (b.last()) {
				        rowCount = b.getRow();
				        b.beforeFirst(); // 将指针移回起始位置
				        response.getWriter().write(String.valueOf(rowCount)+";");
				    }
					b = a.doselect("SELECT t.teacher_id,t.teacher_name,t.teacher_username,t.teacher_password "
							+ "from teacher t "
							+ "LIMIT ?, ?", (currentPage-1)*recordsPerpage, recordsPerpage);
				}
				else {
					b = a.doselect("SELECT s.secretary_id,s.secretary_name,s.secretary_username,s.secretary_password "
							+ "from secretary s ");
					int rowCount = 0;
				    if (b.last()) {
				        rowCount = b.getRow();
				        b.beforeFirst(); // 将指针移回起始位置
				        response.getWriter().write(String.valueOf(rowCount)+";");
				    }
					b = a.doselect("SELECT s.secretary_id,s.secretary_name,s.secretary_username,s.secretary_password "
							+ "from secretary s "
							+ "LIMIT ?, ?", (currentPage-1)*recordsPerpage, recordsPerpage);
				}
			}
			if(user.equals("student")) {
				while(b.next()) {//b代表查询结果，查询之后的那个表，next（）函数就是往下一行，逐行扫描
				response.getWriter().write(b.getString("student_id")+";");//这里表示的是往返回的那个response里面添加字符串，修改write括号后的内容即可
				response.getWriter().write(b.getString("student_name")+";");
				response.getWriter().write(b.getString("student_username")+";");
				response.getWriter().write(b.getString("student_password")+";");
			}//模板
				
			b.close();
			}
			else if(user.equals("teacher")) {
				while(b.next()) {//b代表查询结果，查询之后的那个表，next（）函数就是往下一行，逐行扫描
					response.getWriter().write(b.getString("teacher_id")+";");//这里表示的是往返回的那个response里面添加字符串，修改write括号后的内容即可
					response.getWriter().write(b.getString("teacher_name")+";");
					response.getWriter().write(b.getString("teacher_username")+";");
					response.getWriter().write(b.getString("teacher_password")+";");
				}//模板
					
				b.close();
				}
			else {
				while(b.next()) {//b代表查询结果，查询之后的那个表，next（）函数就是往下一行，逐行扫描
					response.getWriter().write(b.getString("secretary_id")+";");//这里表示的是往返回的那个response里面添加字符串，修改write括号后的内容即可
					response.getWriter().write(b.getString("secretary_name")+";");
					response.getWriter().write(b.getString("secretary_username")+";");
					response.getWriter().write(b.getString("secretary_password")+";");
				}//模板
					
				b.close();
			}
		} catch (SQLException e) {//模板
			e.printStackTrace();//模板
		}//模板}
	}
	}
